﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DataAccessLogic;
using BusinessLogic;

public partial class Admin_UserCreation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
                
    }
    
    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {    
        //lets get the wizard so that we can get the controls out of it
        CreateUserWizard createUserWizard = (CreateUserWizard)sender;
        Constituent constituent = new Constituent();

        try
        {
            //grab the first name and last name controls
            TextBox txtFirstName = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtFirstName");
            TextBox txtLastName = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtLastName");

            //create the constituent to link to the appuser            
            constituent.FirstName = txtFirstName.Text;
            constituent.LastName = txtLastName.Text;
            constituent.Create();

            //get the id of the newly created user using the username
            AppUser appUser = new AppUser();
            appUser.MembershipUser = Membership.GetUser(createUserWizard.UserName);
            appUser.Constituent = constituent;
            appUser.Create();
        }
        catch(Exception ex)
        {
            Membership.DeleteUser(createUserWizard.UserName);
            constituent.Delete();

            Master.lblMessage.Text = ex.Message;
        }
    }
}